﻿using System;
using System.Data;
using BLL;
using Ext.Net;
using System.Xml;
using System.Xml.Xsl;
using Roles;

namespace ERP.Modules.Statistic
{
    public partial class Statistics : System.Web.UI.UserControl
    {
        #region[Define]
        private DataTable dtGetOrderIsUsed;
        private DataTable dtOrderIsUsedDetail;
        private DataTable dtTotalDetail;
        private readonly Order_BLL clsOrder = new Order_BLL();
        private readonly Order_Detail_BLL clsOrderDetail = new Order_Detail_BLL();
        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                CheckPermisstion();
                GetOrderIsUsed();
            }
        }
        //-------Store-Datasource-----------------
        protected void StoreOrderStatistic_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            GetOrderIsUsed();
        }
        protected void StoreOrderStatistic_Submit(object sender, StoreSubmitDataEventArgs e)
        {
            string format = FormatTypeOrder.Value.ToString();
            XmlNode xml = e.Xml;
            Response.Clear();

            switch (format)
            {
                case "xml":
                    string strXml = xml.OuterXml;
                    Response.AddHeader("Content-Disposition", "attachment; filename=submittedData.xml");
                    Response.AddHeader("Content-Length", strXml.Length.ToString());
                    Response.ContentType = "application/xml";
                    Response.Write(strXml);
                    break;

                case "xls":
                    Response.ContentType = "application/vnd.ms-excel";
                    Response.AddHeader("Content-Disposition", "attachment; filename=submittedData.xls");
                    var xtExcel = new XslCompiledTransform();
                    xtExcel.Load(Server.MapPath("Excel.xsl"));
                    xtExcel.Transform(xml, null, Response.OutputStream);

                    break;

                case "csv":
                    Response.ContentType = "application/octet-stream";
                    Response.AddHeader("Content-Disposition", "attachment; filename=submittedData.csv");
                    var xtCsv = new XslCompiledTransform();
                    xtCsv.Load(Server.MapPath("Csv.xsl"));
                    xtCsv.Transform(xml, null, Response.OutputStream);

                    break;
            }
            Response.End();
        }
        protected void StoreOrderDetail_RefreshData(object sender, StoreReadDataEventArgs e)
        {

        }
        //--------Tool-Bar-Main-------------------
        protected void btnDel_Click(object sender, DirectEventArgs e)
        {
            //var sm = this.grOrderStatistic.GetSelectionModel() as CheckboxSelectionModel;
            //foreach (SelectedRow row in sm.SelectedRows)
            //{
            //    try
            //    {
            //        if (clsOrder.CheckDelOrder(int.Parse(row.RecordID.ToString())) == false)
            //        {
            //            Order_BLL.Delete_OrderStatics(int.Parse(row.RecordID.ToString()));
            //            HTML.clsFormat.Alert_INFO("Thông Báo", "Xoá đơn hàng thành công !");
            //        }
            //        else
            //        {
            //            HTML.clsFormat.Alert_ERROR("Thông Báo", "Đơn hàng này còn tồn tại.<br/>" + "Bạn không thể xoá đơn hàng này !");
            //        }
            //    }
            //    catch (Exception ex)
            //    {
            //        HTML.clsFormat.Alert_ERROR("Thông Báo", "Có lỗi xảy ra trong quá trình xoá mẫu tin !" + "<br/>" + ex.ToString());
            //    }
            //}
            //GetOrderIsUsed();
        }
        //----------Row-Command-------------------
        
        //-----------Double_Click---------------------
        protected void DbClick_db(object sender, DirectEventArgs e)
        {
            int Order_Id = int.Parse(e.ExtraParams["Order_Id"]);
            GetDataOrderReview(Order_Id);
            WindowOrderDetail.Show();
            TotalDetail(int.Parse(HdReviewOrder_Id.Value.ToString()));
        }
        //--------Tool-Bar-Window-----------------
        protected void btnExitDetailOrder_Click(object sender, DirectEventArgs e)
        {
            WindowOrderDetail.Close();
        }
        #endregion

        #region[Method]
        protected void GetOrderIsUsed()
        {
            dtGetOrderIsUsed = clsOrder.GetOrderIsUsed(string.Empty);
            if (dtGetOrderIsUsed != null && dtGetOrderIsUsed.Rows.Count > 0)
            {
                StoreOrderStatistic.DataSource = dtGetOrderIsUsed;
                StoreOrderStatistic.DataBind();
            }
            else
            {
                grOrderStatistic.GetStore().RemoveAll();
            }
        }
        protected void GetDataOrderReview(int id)
        {
            dtOrderIsUsedDetail = clsOrderDetail.GetOrderDetail(id);
            if (dtOrderIsUsedDetail != null && dtOrderIsUsedDetail.Rows.Count > 0)
            {
                StoreOrderDetail.DataSource = dtOrderIsUsedDetail;
                StoreOrderDetail.DataBind();
                if ((bool)dtOrderIsUsedDetail.Rows[0]["IsActiveOrder"])
                {
                    lbReViewTime.IconCls = "x-icon-tick";
                    lbReviewDateUpdate.IconCls = "x-icon-tick";
                }
                if ((bool)dtOrderIsUsedDetail.Rows[0]["IsActiveOrder"] == false)
                {
                    lbReViewTime.IconCls = "x-icon-cross";
                    lbReviewDateUpdate.IconCls = "x-icon-cross";
                }
                HdReviewOrder_Id.Text = dtOrderIsUsedDetail.Rows[0]["Order_Id"].ToString();
                HdReviewIsActive.Text = dtOrderIsUsedDetail.Rows[0]["IsActiveOrder"].ToString();
                lbReviewCustomer_Alias.Text = dtOrderIsUsedDetail.Rows[0]["Customer_Alias"].ToString();
                lbReviewCustomer_Name.Text = dtOrderIsUsedDetail.Rows[0]["Customer_Name"].ToString();
                lbReviewOrderCode.Text = dtOrderIsUsedDetail.Rows[0]["Order_Code"].ToString();
                lbReviewOrderDate.Text = String.Format("{0:dd-MM-yyyy}", dtOrderIsUsedDetail.Rows[0]["Order_DateBegin"]);
                if (dtOrderIsUsedDetail.Rows[0]["DateReview"].ToString() == string.Empty || dtOrderIsUsedDetail.Rows[0]["DateReview"].ToString() == DBNull.Value.ToString())
                {
                    lbReviewDateUpdate.Text = "N/A";
                }
                else
                {
                    lbReviewDateUpdate.Text = String.Format("{0:dd-MM-yyyy}", dtOrderIsUsedDetail.Rows[0]["DateReview"]);
                }
                if (dtOrderIsUsedDetail.Rows[0]["DateReview"].ToString() == string.Empty || dtOrderIsUsedDetail.Rows[0]["DateReview"].ToString() == DBNull.Value.ToString())
                {
                    lbReViewTime.Text = "N/A";
                }
                else
                {
                    lbReViewTime.Text = String.Format("{0:HH:mm:ss}", dtOrderIsUsedDetail.Rows[0]["DateReview"]);
                }
            }
            else
            {
                grOrderDetail.GetStore().RemoveAll();
            }
        }
        protected void TotalDetail(int id)
        {
            dtTotalDetail = clsOrderDetail.GetOrderDetailReviewSum(id);
            if (dtTotalDetail != null && dtTotalDetail.Rows.Count > 0)
            {
                lbTotalMaterialReview.Text = String.Format("{0:0,00}", dtTotalDetail.Rows[0]["TotalCost"]).Replace(".", ",");
            }
        }
        #endregion

        #region[Search]
        protected void Search_Event(object sender, DirectEventArgs e)
        {
            dtGetOrderIsUsed = clsOrder.GetOrderIsUsed(txtSearch_Statistic.Text);
            if (dtGetOrderIsUsed != null && dtGetOrderIsUsed.Rows.Count > 0)
            {
                StoreOrderStatistic.DataSource = dtGetOrderIsUsed;
                StoreOrderStatistic.DataBind();
            }
            else
            {
                grOrderStatistic.GetStore().RemoveAll();
            }
        }
        #endregion

        #region[Permisstion]
        private void CheckPermisstion()
        {
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.Customer, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                grOrderStatistic.Visible = Is_BtnView;
                btnDel.Visible = Is_BtnDel;
                btnExport.Visible = Is_BtnPrint; 
            }
        }
        #endregion
    }
}